package com.jprocms.module.cms.dal.mysql.channel;

import com.jpro.framework.datapermission.core.annotation.DataPermission;
import com.jpro.framework.mybatis.core.mapper.BaseMapperX;
import com.jpro.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.jprocms.module.cms.controller.admin.channel.vo.ChannelFrontListReqVO;
import com.jprocms.module.cms.controller.admin.channel.vo.ChannelListReqVO;
import com.jprocms.module.cms.controller.admin.channel.vo.ChannelSimpleReqVO;
import com.jprocms.module.cms.controller.admin.channel.vo.ContentChannelListReqVO;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 栏目 Mapper
 * includeRules = {ChannelPermissionRule.class}
 *
 * @author jprocms
 * @DataPermission(includeRules = {ChannelPermissionRule.class}) 对于栏目类值需要限定栏目权限检查
 * 否则有DeptData权限检查则会报错 因为栏目没有dept_id字段
 */
@Mapper
public interface ChannelMapper extends BaseMapperX<ChannelDO> {

    @Override
    @DataPermission(enable = false)
        // 禁用数据权限，避免取父栏目取不到
    ChannelDO selectById(Serializable id);

    List<ChannelDO> selectChannelList(@Param("params") ChannelListReqVO params);

    List<ChannelDO> selectChannelListForFront(@Param("params") ChannelListReqVO params);

    List<ChannelDO> selectByChannelPath(@Param("params") ChannelFrontListReqVO params);


    List<ChannelDO> selectByChannelIds(@Param("ids") Collection<Long> ids);

    default List<ChannelDO> selectContentChannelList(ContentChannelListReqVO reqVO) {
        final LambdaQueryWrapperX<ChannelDO> lambdaQueryWrapperX = new LambdaQueryWrapperX<ChannelDO>()
                .eqIfPresent(ChannelDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(ChannelDO::getRecycleStatus, reqVO.getRecycleStatus())
                .eqIfPresent(ChannelDO::getParentId, reqVO.getParentId());
        lambdaQueryWrapperX.likeIfPresent(ChannelDO::getName, reqVO.getName());
        if (StringUtils.hasText(reqVO.getKey())) {
            lambdaQueryWrapperX.and(queryWrapper -> queryWrapper.or().eq(ChannelDO::getId, reqVO.getKey())
                    .or().eq(ChannelDO::getPath, reqVO.getKey()).or().like(ChannelDO::getName, reqVO.getName()));
        }
        lambdaQueryWrapperX.orderByAsc(ChannelDO::getSort);
        return selectList(lambdaQueryWrapperX);
    }

    /***
     * 用在栏目下拉列表，没有权限过滤注解
     * @param reqVO
     * @return
     */
    default List<ChannelDO> selectListForOptions(ChannelSimpleReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ChannelDO>()
                .eqIfPresent(ChannelDO::getSiteId, reqVO.getSiteId())
                .eqIfPresent(ChannelDO::getRecycleStatus, reqVO.getRecycleStatus())
                .likeIfPresent(ChannelDO::getName, reqVO.getName())
                .orderByAsc(ChannelDO::getSort));
    }

    default List<ChannelDO> selectListByProcessDefinitionId(String processDefinitionId) {
        return selectList(ChannelDO::getProcessDefinitionId, processDefinitionId);
    }

    @Override
    @DataPermission(enable = false)
    default Boolean updateBatch(Collection<ChannelDO> entities) {
        return BaseMapperX.super.updateBatch(entities);
    }

    @Override
    @DataPermission(enable = false)
    default Boolean updateBatch(Collection<ChannelDO> entities, int size) {
        return BaseMapperX.super.updateBatch(entities, size);
    }
}
